/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clusterAnalysis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import units.CellType;
import units.MonkeyType;
import utils.FileUtils;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class BasicClusterNetworkFigure {


    private File outputFolder = new File("/Users/jamie/Dropbox/monkeys/clust/basicNetworks/");

    private int[] clusterIds = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

    public void outputIntersectionNetwork(){
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql;
        sql = "select * from clusterIntersections where fdr<0.05";
        ResultSet rs = handle.runSQLquery(sql);
        List<Integer> included = new ArrayList<Integer>();
        BufferedWriter writer = FileUtils.getBufferedWriter(new File(outputFolder, "intersectionsNetwork.txt"));
        try {
            writer.append("clusterId_a\tclusterId_b\tsources\tmcc\n");
        } catch (IOException ex) {
            Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (rs.next()) {

                int clusterId_a = rs.getInt("clusterId_a");
                MonkeyType mt_a = MonkeyType.valueOf(rs.getString("monkeyType_a"));
                CellType ct_a = CellType.valueOf(rs.getString("cellType_a"));
                String source_a = mt_a.name()+ct_a.name();

                int clusterId_b = rs.getInt("clusterId_b");
                MonkeyType mt_b = MonkeyType.valueOf(rs.getString("monkeyType_b"));
                CellType ct_b = CellType.valueOf(rs.getString("cellType_b"));
                String source_b = mt_b.name()+ct_b.name();

                double mcc = rs.getDouble("mcc");
                try {
                    writer.append(clusterId_a + "\t" + clusterId_b + "\t" + source_a + "_" + source_b + "\t" + mcc + "\n");
                } catch (IOException ex) {
                    Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(!included.contains(clusterId_a)){
                    included.add(clusterId_a);
                }
                if(!included.contains(clusterId_b)){
                    included.add(clusterId_b);
                }
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
        }

        for(int clusterId : clusterIds){
            if(!included.contains(clusterId)){
                try {
                    writer.append(clusterId + "\t100\tx\t0\n");
                } catch (IOException ex) {
                    Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        try {
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void outputMinetClusterNetwork(){
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql;
        sql = "select * from minetClusterStats where meanFDR<0.05";
        ResultSet rs = handle.runSQLquery(sql);
        List<Integer> included = new ArrayList<Integer>();
        BufferedWriter writer = FileUtils.getBufferedWriter(new File(outputFolder, "minetNetwork.txt"));
        try {
            writer.append("clusterId_a\tclusterId_b\tmeanMV\n");
        } catch (IOException ex) {
            Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (rs.next()) {

                int clusterId_a = rs.getInt("clusterId_a");
                int clusterId_b = rs.getInt("clusterId_b");


                double mv = rs.getDouble("meanMV_real");
                try {
                    writer.append(clusterId_a + "\t" + clusterId_b + "\t" + -Math.log10(mv)+ "\n");
                } catch (IOException ex) {
                    Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(!included.contains(clusterId_a)){
                    included.add(clusterId_a);
                }
                if(!included.contains(clusterId_b)){
                    included.add(clusterId_b);
                }
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
        }

        for(int clusterId : clusterIds){
            if(!included.contains(clusterId)){
                try {
                    writer.append(clusterId + "\t100\t0\n");
                } catch (IOException ex) {
                    Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        try {
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(BasicClusterNetworkFigure.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
